const FORMAT = require('../../api/util.js')
const API = require('../../api/api.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    maxDate: new Date(2099, 12, 31).getTime(),
    customType: "",
    elements: [{
        title: '自定时光瓶',
        name: '自选时间',
        color: 'black',
        days: "custom"
      },
      {
        title: '契约时光瓶',
        name: '自定契约时光瓶',
        color: 'black',
        days: "custom"
      },
      {
        title: '小目标',
        name: '7天小时光瓶',
        color: 'orange',
        days: 7
      },
      {
        title: '小习惯',
        name: '21天小习惯',
        color: 'red',
        days: 21
      },
      {
        title: '月度时光瓶',
        name: '30天时光瓶',
        color: 'brown',
        days: 30
      },
      {
        title: '半年时光瓶',
        name: '180天时光瓶',
        color: 'brown',
        days: 180
      },
      {
        title: '年度时光瓶',
        name: '365天时光瓶',
        color: 'brown',
        days: 365
      },
      {
        title: '5年时光瓶',
        name: '5年时光瓶',
        color: 'brown',
        days: 1825
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '时光瓶',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  handleClick: function (e) {
    let item = e.currentTarget.dataset.value
    let today = new Date().getTime() / 1000
    let day = ""
    let userInfo = wx.getStorageSync('userInfo')
    if (item.days == "custom") {
      this.data.customType = item.title
      this.setData({
        show: true
      })
    } else {
      API.checkCountNumber('shiguangping', userInfo._openid).then(res => {
        if (res.total < userInfo.shiGuangPingShuLiang){
          day = FORMAT.formatDay(Math.floor(today + item.days * 86400))
          wx.navigateTo({
            url: "/pages/shiguangping/shiguangping?type=" + item.title + "&day=" + day + "&color=" + item.color,
          })
        }else{
          wx.showToast({
            title: '您已经不能再设置时光瓶的，目标太多也吃不消啊~~~',
            icon: 'none',
            mask: true
          })
        }
      })
    }
  },

  onClose: function () {
    this.setData({
      show: false
    })
  },

  onInput: function (e) {
    this.setData({
      currentDate: e.detail,
    });
  },

  onConfirm: function (e) {
    let time = e.detail / 1000
    let day = FORMAT.formatDay(Math.floor(time))
    let userInfo = wx.getStorageSync('userInfo')
    API.checkCountNumber('shiguangping', userInfo._openid).then(res => {
      if (res.total < userInfo.shiGuangPingShuLiang){
        wx.navigateTo({
          url: "/pages/shiguangping/shiguangping?type=" + this.data.customType + "&day=" + day
        })
        this.setData({
          show: false
        })
      }else{
        wx.showToast({
          title: '您已经不能再设置时光瓶的，目标太多也吃不消啊~~~',
          icon: 'none',
          mask: true
        })
      }
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index],
      path: '/pages/index/index'
    }
  },
  onShareTimeline: function () {
let shareimages = app.globalData.shareimages
    let index = Math.floor(Math.random() * 5)
    return {
      title: '一个真正匿名的心情小程序',
      imageUrl: shareimages[index]
    }
  }
})